package com.eigaland.change_machine.business.selectseat

import com.eigaland.change_machine.BuildConfig
import com.eigaland.change_machine.business.network.RetrofitClient
import com.eigaland.change_machine.displaymodel.SelectSeatDisplayModel
import com.eigland.eigaland_lib.base.BaseRepository
import com.eigland.eigaland_lib.model.Seat
import com.eigland.eigaland_lib.model.SeatPlan
import com.eigland.eigaland_lib.model.Section
import com.eigland.eigaland_lib.network.MovieService
import com.orhanobut.logger.Logger
import kotlin.math.abs

public class SelectSeatFragmentRepository : BaseRepository() {


   suspend fun loadSeatData(showId:String):SelectSeatDisplayModel{
       var selectSeat = SelectSeatDisplayModel()
       var result =  RetrofitClient.getService(MovieService::class.java, BuildConfig.domain).seatPlanByShowId(showId).await()
        result?.success?.seatingPlan?.sections?.map {section ->
                var delay = 0
                var realW = section.w
                section.labels.forEach {
                    if (it.y < delay) {
                        delay = it.y
                    }
                    if (it.w!! + it.x > realW) {
                        realW= it.w!! + it.x
                    }
                }
                section.seats.forEach {
                    if (it.y < delay) {
                        delay = it.y
                    }
                    if(it.w==null)it.w=50
                    if(it.h==null)it.h=50
                    if (it.w!! + it.x > realW) {
                        realW=it.w!! + it.x
                    }
                }

                if(realW>section.w){
                    section.w=realW
                }

                if(realW> result?.success?.seatingPlan!!.w){
                    result?.success?.seatingPlan!!.w=realW
                }

                if (delay < 0) {//correct
                    section.y = section.y - abs(delay)
                    section.h = section.h + abs(delay)

                    section.seats.forEach {
                        it.y = it.y + abs(delay)
                    }

                    section.labels.forEach {
                        it.y = it.y + abs(delay)
                    }
                }


            }
       selectSeat.seatPlan = result?.success?.seatingPlan
       return selectSeat
     }

    private fun SeatPlan.copyDefault(): SeatPlan =
        copy(sections = sections.map { section: Section ->
            section.copy(
                seats = section.seats.map { seat ->
                    seat.copy(
                        w = seat.w ?: defSeatW,
                        h = seat.h ?: defSeatH,
                        color = seat.color ?: defSeatColor,
                        bgColor = seat.bgColor ?: defSeatBgColor,
                        colorSelected = seat.colorSelected ?: defSeatColorSelected,
                        bgColorSelected = seat.bgColorSelected
                            ?: defSeatBgColorSelected,
                        colorReserved = seat.colorReserved ?: defSeatColorReserved,
                        bgColorReserved = seat.bgColorReserved
                            ?: defSeatBgColorReserved,
                        colorSold = seat.colorSold ?: defSeatColorSold,
                        bgColorSold = seat.bgColorSold ?: defSeatBgColorSold,
                        bgImg = seat.bgImg ?: defSeatBgImg,
                        bgImgSelected = seat.bgImgSelected ?: defSeatBgImgSelected,
                        textSize = seat.textSize ?: defSeatFontSize,
                        isSelected = false,
                        availability = seat.availability

                    )

                },


                labels = section.labels.map {
                    it.copy(
                        color = it.color ?: defLabelColor,
                        w = it.w ?: defLabelW,
                        h = it.h ?: defLabelH,
                        fontSize = it.fontSize ?: defLabelFontSize,
                        bgColor = it.bgColor ?: "#00FFFFFF",
                        bgImg = it.bgImg ?: "",
                        text = it.text
                    )
                }


            )
        }
        )
}